{extend name="public/layout" /}
{block name="main"}
<style>
    .hha{
        text-align: center;
    }
    .layui-table-cell{
        /*display:table-cell;*/
        /*vertical-align: middle;*/
        height:auto;
        /*line-height: normal;*/
    }
    /*.layui-table-body table tr td .laytable-cell-1-0-0{*/
    /*    display: none;*/
    /*}*/
    .laytable-cell-1-0-0{
        /*display: none;*/
    }
    .myinput{
        height: 35px;
        line-height: 35px;
    }
    .layui-form-label{
        /*line-height: 35px;*/
    }
</style>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <div class="layui-row" style="reloadwidth: 100%;margin-top: 15px;min-height: 40px">
        {include file='table/toolbarDemo' /}
    </div>
</fieldset>
<table class="layui-table" style="width: 100%;" lay-data="{height: 'full-200',cellMinWidth: 10,url:'{$over['action']|default=''}', page:{layout:['count', 'prev','page','next','limit','refresh','skip']},limits:[10,20,30,40,50,60,70,80,90,100,110,120,130,140,150], id:'idTest',totalRow:true}" lay-filter="demo">
<thead>
<tr>
    <th lay-data="{type:'checkbox',field:'id',align:'center'}"></th>
    <th lay-data="{field:'id', align:'center'}" >ID</th>
    <th lay-data="{field:'cess', align:'center',totalRow:true}" >cess</th>
    <th lay-data="{fixed: 'right',width:{$over['rightheader']['width']}, align:'center', toolbar: '#barDemo',align:'center'}">{$over['rightheader']['desc']|default='操作'}</th>
</tr>
</thead>
</table>
<script type="text/html" id="barDemo">
    {foreach $button as $key=>$value}
    {if $key!=0&&$key%2==0}
    <br>
    {/if}
    {if $value['type']=='button'}
    <a class="layui-btn layui-btn-xs {$value['class']}" style="{$value['style']}" lay-event="right"  data-id="{{d.id}}" data-action="{$value['action']}" >{$value['desc']|default='按钮'}</a>
    {/if}

    {if $value['type']=='edit'}
    <a class="layui-btn layui-btn-xs {$value['class']}" style="{$value['style']}" lay-event="edit"  data-id="{{d.id}}" data-action="{$value['action']}" >{$value['desc']|default='编辑'}</a>
    {/if}

    {if $value['type']=='add'}
    <a class="layui-btn layui-btn-xs {$value['class']}" style="{$value['style']}" lay-event="edit"  data-id="{{d.id}}" data-action="{$value['action']}" >{$value['desc']|default='添加'}</a>
    {/if}

    {if $value['type']=='detail'}
    <a class="layui-btn layui-btn-primary layui-btn-xs {$value['class']}" style="{$value['style']}" lay-event="detail"  data-id="{{d.id}}" data-action="{$value['action']}" >{$value['desc']|default='查看'}</a>
    {/if}

    {if $value['type']=='del'}
    <a class="layui-btn layui-btn-danger layui-btn-xs {$value['class']}" style="{$value['style']}" lay-event="del"  data-id="{{d.id}}" data-action="{$value['action']}" >{$value['desc']|default='删除'}</a>
    {/if}

    {if $value['type']=='a'}
    <a class="layui-btn  layui-btn-normal layui-btn-xs {$value['class']}" style="{$value['style']}" lay-event="right"  data-id="{{d.id}}" hrefs="{$value['action']}?id={{d.id}}" >{$value['desc']|default='跳转'}</a>
    {/if}

    {/foreach}
</script>
{include file="table/template" /}
{/block}
{block name="script"}
<script>
    layui.use(['table','form'],function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var times = $('.times');
        var timesid = '';
        $.each(times,function (i,v) {
            console.log(v);
            timesid = $(v).attr('id');
            laydate.render({
                elem: '#'+timesid //指定元素
                ,'type':'datetime'
            });
        })
        // laydate.render({
        //     elem: '#time2' //指定元素
        // });

        function souqi(){
            var yao = "{$woqu|default='0'}";
            console.log(yao);
            if(yao=='1'){
                var suoyou = $("tbody span[fid!='0']").parents('tr');
                $.each(suoyou,function (i,v) {
                    var data_index=$(v).attr('data-index');
                    console.log(v);
                    // console.log($(v).attr('data-index'));
                    $("tbody tr[data-index="+data_index+"]").hide();
                })
            }
        }


        table.reload("idTest",{
            done:function () {
                souqi();
            }
        })



        $(document).on('click','.x-show',function () {
            if($(this).attr('status')==='true'){
                $(this).children('i').html('&#xe625;');
                $(this).attr('status','false');
                var ids = $(this).attr('ids');
                var quanbu = $('.x-show[fid='+ids+']');
                console.log(quanbu);
                $.each(quanbu,function (i,v) {
                    var data_index = $(v).parents('tr').attr('data-index');
                    $("tbody tr[data-index="+data_index+"]").show();
                });
            }else{
                $(this).children('i').html('&#xe623;');
                $(this).attr('status','true');
                var cateId = $(this).attr('ids');
                getcateId(cateId);
            }
        })
        function getcateId(cate){
            var quanbu = $('.x-show[fid='+cate+']');
            console.log(quanbu);
            $.each(quanbu,function (i,v) {
                var data_index = $(v).parents('tr').attr('data-index');
                getcateId($(v).attr('ids'));
                $(v).attr('status','true');
                $(v).children('i').html('&#xe623;');
                $("tbody tr[data-index="+data_index+"]").hide();
            })
        }

        /**
         * 重构表格
         */
        function onreloader() {
            var tiaojians = $("#myforms").serializeArray();
            // console.log(tiaojian);
            var tiaojian = {};
            $.each(tiaojians,function (i,v) {
                tiaojian[v.name] = v.value;
            })



            // var tiaojian = {};
            // var tiji = $("#sousuo").val();
            // var ntype = $("#sousuos").val();
            // var timefild = $("#timefild").val();
            // var start = $("#time1").val();
            // var end = $("#time2").val();
            // if(timefild){
            //     tiaojian.timefild = timefild;
            //     tiaojian.start = start;
            //     tiaojian.end = end;
            // }
            // var cid = '{$over["cid"]|default=""}';
            // if(cid){
            //     tiaojian.cid = cid;
            // }else{
            //     var model_id = '{$over["model_id"]|default=""}';
            //     if(model_id){
            //         tiaojian.model_id = model_id;
            //     }
            // }
            // tiaojian.tiaojian = tiji;
            // if(ntype){
            //     tiaojian.field = ntype;
            // }
            // var curr = $(".layui-laypage-curr").children('em').eq(1).text();
            // console.log(curr);
            // if(curr){
            //     tiaojian.page = 1;
            // }
            // tiaojian.page = 1;
            console.log(tiaojian);
            // return;
            var ispage = '{$over["page"]??null}';
            if(ispage){
                var curr = $(".layui-laypage-curr").children('em').eq(1).text();
                // if(curr){
                //     tiaojian.page = 1;
                // }
                // tiaojian.page = 1;
                // console.log(curr);
                table.reload('idTest', {
                    page: {
                        curr: curr //重新从第 1 页开始
                    }
                    ,where: tiaojian,
                    done:function () {
                        souqi();
                    }
                });
            }else{
                tiaojian.limit = 1000;
                table.reload('idTest', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: tiaojian,
                    done:function () {
                        souqi();
                    }
                });
                $(".layui-table-page").hide();
                // location.reload();
            }
            // if(ispage!==''){
            //     table.reload('idTest', {
            //         page: {
            //             curr: curr //重新从第 1 页开始
            //         }
            //         ,where: tiaojian,
            //         done:function () {
            //             souqi();
            //         }
            //     });
            // }else{
            //     location.reload();
            // }
            // $("[data-page="+tiaojian.page+"]").click();
        }

        /**
         * 监听编辑input
         */
        table.on('edit',function (obj) {
            console.log(obj);
            // console.log($(this))
            var tiaojian = {};
            var id = obj.data.id;
            if(id){
                tiaojian.id = id;
            }
            var field = obj.field;
            if(field){
                tiaojian.field = field;
            }
            var value = obj.value;
            if(value){
                tiaojian.value = value;
            }
            var action = '{$monitoraction["edit"]|default=""}';
            var cid = '{$over["cid"]|default=""}';
            if(cid){
                tiaojian.cid = cid;
            }
            var model_id = '{$over["model_id"]|default=""}';
            if(model_id){
                tiaojian.model_id = model_id;
            }
            // console.log(action);
            // return
            if(action){
                var index = layer.load(0, {shade: false});
                $.post(action,tiaojian,function (res) {
                    console.log(res)
                    layer.close(index);
                    layer.msg(res.msg);
                    setTimeout(function () {
                        onreloader();
                    },1500)
                })
            }
        })

        /**
         * 监听行间的按钮，如查看，删除，修改
         * */
        table.on('tool(demo)',function (obj) {
            if(obj.event=='right'){
                layer.open({
                    type: 2,
                    title: false,
                    area:['90%','80%'],
                    content:$(this).attr('hrefs')
                });
                return;
            }
            var cid = '{$over["cid"]|default=""}';
            var model_id = '{$over["model_id"]|default=""}';
            var action = $(this).attr('data-action');
            var tiaojian = {data:obj.data};
            var id = obj.data.id;
            if(id){
                tiaojian.id = id;
            }
            var field = $(this).attr('data-field');
            if(field){
                tiaojian.field = field;
            }
            var type = obj.event;
            if(cid){
                tiaojian.cid = cid;
            }
            if(model_id){
                tiaojian.model_id = model_id;
            }
            //  console.log(type);
            //  console.log(action);
            // return
            //  console.log(tiaojian);
            //  return;
            if(action){
                if(Object.keys(tiaojian).length>0){
                    action = action+'?';
                }
                $.each(tiaojian,function (i,v) {
                    action+=i+'='+v+'&';
                })
                if(type=='edit'||type=='detail'){
                    console.log(action);
                    if(type!=='detail'){
                        layer.open({
                            type:2,
                            area:['80%','90%'],
                            content:action,
                            end:function () {
                                onreloader();
                            }
                        })
                    }else{
                        layer.open({
                            type:2,
                            area:['80%','90%'],
                            content:action,
                            end:function () {

                            }
                        })
                    }
                }else{
                    if(type=='del'){
                        layer.confirm('确定要进行这个操作吗？', {
                            btn: ['确定','取消'] //按钮
                        }, function(){
                            var index = layer.load(0, {shade: false});
                            $.post(action,tiaojian,function (res) {
                                console.log(res);
                                layer.msg(res.msg);
                                layer.close(index);
                                if(type!=='detail'){
                                    setTimeout(function () {
                                        onreloader();
                                    },1500)
                                }
                            })
                            return;
                        });
                        return
                    }
                    var index = layer.load(0, {shade: false});
                    $.post(action,tiaojian,function (res) {
                        layer.close(index);
                        layer.msg(res.msg);
                        if(type!=='detail'){
                            setTimeout(function () {
                                onreloader();
                            },100)
                        }
                    })
                }
            }
        })
        var active = {
            //监听全删除
            alldel:function (obj) {
                // console.log('全删除')
                var action = $(this).attr('data-action');
                var checkStatus = table.checkStatus('idTest');
                var cid = '{$over["cid"]|default=""}';
                var tiaojian = {data:checkStatus.data};
                if(cid){
                    tiaojian.cid = cid;
                }
                var model_id = '{$over["model_id"]|default=""}';
                if(model_id){
                    tiaojian.model_id = model_id;
                }
                var id = [];
                $.each(checkStatus.data,function (i,v) {
                    id.push(v.id);
                });
                if(id.length>0){
                    tiaojian.id = id;
                }else{
                    layer.msg('请选择数据!!');
                    return
                }
                layer.confirm('确定要执行这个操作吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    var index = layer.load(0, {shade: false});
                    console.log(tiaojian);
                    $.post(action,tiaojian,function (res) {
                        console.log(res);
                        layer.close(index);
                        layer.msg(res.msg);
                        onreloader();
                    })
                    return;
                });
                // console.log(tiaojian);
            },
            //监听按钮
            button:function(obj){
                var action = $(this).attr('data-action');
                var cid = '{$over["cid"]|default=""}';
                var model_id = '{$over["model_id"]|default=""}';
                console.lgo(action)
                console.lgo(cid)
                console.lgo(model_id)
            },
            //监听添加
            add:function (obj) {
                var action = $(this).attr('data-action');
                var cid = '{$over["cid"]|default=""}';
                var model_id = '{$over["model_id"]|default=""}';
                var str = '';
                if(cid){
                    if(str){
                        str+='cid='+cid;
                    }else{
                        str+='?cid='+cid;
                    }
                }
                if(model_id){
                    if(str){
                        str+='model_id='+model_id;
                    }else{
                        str+='?model_id='+model_id;
                    }
                }
                layer.open({
                    type:2,
                    area:['80%','90%'],
                    content:action+str,
                    end:function () {
                        onreloader();
                    }
                })
            },
            //监听返回
            back:function (obj) {
                history.back();
            },
            //监听搜索
            reload:function (obj) {
                onreloader();
            },
            export:function (obj) {
                var tiaojian = {};
                var tiji = $("#sousuo").val();
                var ntype = $("#sousuos").val();
                var timefild = $("#timefild").val();
                var start = $("#time1").val();
                var end = $("#time2").val();
                var url = $(this).attr('data-action');
                if(tiji){
                    tiaojian.tiaojian = tiji;
                }
                if(ntype){
                    tiaojian.field =ntype;
                }
                if(timefild){
                    tiaojian.timefild = timefild;
                }
                if(start){
                    tiaojian.start = start;
                }
                if(end){
                    tiaojian.end = end;
                }
                var index  = 0;
                $.each(tiaojian,function (i,v) {
                    if(index==0){
                        url = url+'?'+i+'='+v;
                    }else{
                        url = url+'&'+i+'='+v;
                    }
                    index++;
                })
                console.log(url);
                window.open(url);
            }
        }

        $('.demoTable').on('click', function(){
            var type = $(this).attr('lay-event');
            // console.log(type);
            active[type] ? active[type].call(this) : '';
        });
        $(".demoTables").on('change',function () {
            console.log('改变')
            onreloader();
        })
        /**
         * 监听开关
         */
        form.on('switch', function(obj){
            var tiaojian = {};
            var id = $(this).attr('value');
            if(id){
                tiaojian.id = id;
            }
            var field = $(this).attr('field');
            if(field){
                tiaojian.field = field;
            }
            var action = '{$monitoraction["switch"]|default=""}';
            var cid = '{$over["cid"]|default=""}';
            if(cid){
                tiaojian.cid = cid;
            }
            var model_id = '{$over["model_id"]|default=""}';
            if(model_id){
                tiaojian.model_id = model_id;
            }
            console.log(action);
            if(action){
                var index = layer.load(0, {shade: false});
                $.post(action,tiaojian,function (res) {
                    console.log(res)
                    layer.close(index);
                    layer.msg(res.msg);
                    if(res.code!==200){
                        setTimeout(function () {
                            onreloader();
                        },1500)
                    }
                })
            }

        });

    })
    function tableyulan(obj) {
        var src = $(obj).attr('src');
        layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: ['80%', '90%'], //宽高
            content: '<div style="width: 100%;text-align: center"><img src="'+src+'" /></div>'
        });
    }
</script>
{/block}